#include "logui.h"
#include "ui_logui.h"

logui::logui(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::logui)
{
    ui->setupUi(this);

    ui->groupLog->hide();//隐藏登陆框

    this->setFixedSize(this->size());//锁定窗口大小

    if(ini() == false){//如果初始化数据库失败
        QMessageBox::warning(0,"警告","初始化数据库失败!");
        return;
    }
}

logui::~logui()
{
    delete ui;
}

void logui::on_btnLog_clicked()
{
    if(ui->groupLog->isHidden()){
        ui->groupLog->show();
        return;
    }

    QString account = ui->editAccount->text();
    QString pswd = ui->editPswd->text();
    if(account.simplified().isEmpty()){//如果账号为空
        QMessageBox::information(0,"提示","账号不能为空.");
        return;
    }
    if(pswd.simplified().isEmpty()){//如果密码为空
        QMessageBox::information(0,"提示","密码不能为空.");
        return;
    }

    QSqlQuery query;
    query.exec("select pswd from tb_admin where account='"+account+"';");
    if(query.size() == 0){//如果在管理员表中没有找到该账号则在用户表中查找
        QMessageBox::warning(0,"警告","账号输入错误.");
        return;
    }

    query.first();
    if(query.isValid() && pswd != query.value("pswd").toString()){
        QMessageBox::warning(0,"警告","密码输入错误!");
        return;
    }

    query.exec("select id from tb_admin where account='"+account+"';");
    query.first();
    if(query.isValid() == false){
        QMessageBox::warning(0,"警告","数据库检索出错!");
        return;
    }

    this->close();
    MainWindow* m = new MainWindow(query.value("id").toString(),&m_db,this);
    m->show();

}

bool logui::ini()
{
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setDatabaseName("db_orderSys");
    m_db.setHostName("localhost");
    m_db.setUserName("root");
    m_db.setPassword("KWP-ffmxyr0");
    if(m_db.open() == true){
        return true;//打开数据库成功返回true
    }
    else{
        return false;
    }
}


void logui::on_btnGetOrder_clicked()
{
    this->close();

    getorder* g = new getorder(&m_db);
    g->show();
}

